<?php

class Default_Model_Files extends Light_Db_Table
{
	protected $_table = 'Files';

	public function upload($params)
	{
		if ($_FILES['files']['error']) {
			$code = $_FILES['files']['error'];
			switch ($code) {
				case UPLOAD_ERR_INI_SIZE:
					$message = "The uploaded file exceeds the upload_max_filesize directive in php.ini";
					break;
				case UPLOAD_ERR_FORM_SIZE:
					$message = "The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form";
					break;
				case UPLOAD_ERR_PARTIAL:
					$message = "The uploaded file was only partially uploaded";
					break;
				case UPLOAD_ERR_NO_FILE:
					$message = "No file was uploaded";
					break;
				case UPLOAD_ERR_NO_TMP_PATH:
					$message = "Missing a temporary folder";
					break;
				case UPLOAD_ERR_CANT_WRITE:
					$message = "Failed to write file to disk";
					break;
				case UPLOAD_ERR_EXTENSION:
					$message = "File upload stopped by extension";
					break;
				default:
					$message = "Unknown upload error";
					break;
			}
			return false;
		}

	    if ($_FILES['files']['name']) {
	        @mkdir($params['path'], 0777, true);
	        $name = $_FILES['files']['name'];
	        $ext  = explode('.', $name);
	        $ext  = end($ext);
	        $this->insert(array(
                'document' => $params['document'],
                'type'     => $params['type'],
                'targetId' => $params['targetId'],
                'name'     => $name
            ));
	        move_uploaded_file($_FILES['files']['tmp_name'], LIGHT_FILES_UPLOAD_PATH . $this->getLastId() . '.' . $ext);
	        return true;
	    }

	    return false;
	}

	public function delete($id)
	{
	    $item = $this->find($id)->current();

	    if ($item !== null) {
	        $ext  = explode('.', $item->name);
	        $ext  = end($ext);
	        @unlink(LIGHT_FILES_UPLOAD_PATH . '/' . $item->id . '.' . $ext);
	        $item->delete();
	    }
	}

	public function download($id)
	{
	    $item = $this->find($id)->current();

	    if ($item !== null) {
            $ext  = explode('.', $item->name);
            $ext  = end($ext);
            $path = LIGHT_FILES_UPLOAD_PATH . '/' . $item->id . '.' . $ext;
			if (!is_readable($path)) {
				header('Location: ' . $_SERVER['HTTP_REFERER']);
				return;
			}
            header("Content-Type: application/force-download");
            header("Content-Type: application/octet-stream");
            header("Content-Type: application/download");
            header("Content-Length: " . filesize($path));
            header("Content-Disposition: attachment; filename=\"{$item->name}\"");
            header("Pragma: public");
            header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
            set_time_limit(0);
            $file = @fopen($path, "rb");
            while(!feof($file)) {
                print(@fread($file, 1024*8));
                ob_flush();
                flush();
            }
        }
	}
}